﻿using System;
using System.Text;
using System.Globalization;

namespace SBPweb.PhotoGallery.Common
{

    /// <summary>
    /// Summary description for PhotoGallerySlideInfo
    /// </summary>
    public class Slide : IComparable<Slide>
    {

        #region Private fields

        private Uri pImageUri;
        private Uri pThumbUri;
        private string pCaption;
        private int? pDuration;
        private int? pEndDelay;
        private int? pAnimationBegin;
        private int? pAnimationEnd;
        private double? pCenterX;
        private double? pCenterY;
        private double? pScale = 0.9f;
        private double? pRotateAngle;
        private bool? pBorderVisible;
        private string pBorderColor;
        private int? pBorderWidth;
        private int? pBorderRadius;
        private int pOrderIndex;
        //private int pSlideId;
        //private int pGalleryId;

        private static IFormatProvider NumberFormatProvider = new CultureInfo("en-US").NumberFormat;

        #endregion

        #region Constructor logic

        public Slide(string imageUrl, string thumbUrl)
            : this(imageUrl, thumbUrl, null)
        {
        }

        public Slide(string imageUrl, string thumbUrl, string caption)
        {
            CreateInstance(imageUrl, thumbUrl, caption);
        }

        public Slide(Uri imageUrl, Uri thumbUrl)
            : this(imageUrl, thumbUrl, null)
        {
        }

        public Slide(Uri imageUrl, Uri thumbUrl, string caption)
        {
            CreateInstance(imageUrl, thumbUrl, caption);
        }

        public Slide(
            Uri imageUrl,
            Uri thumbnailUrl,
            string caption, 
            int? duration,
            int? endDelay,
            int? animationBegin,
            int? animationEnd,
            double? centerX,
            double? centerY,
            double? scale,
            double? rotateAngle,
            bool? borderVisible,
            string borderColor,
            int? borderWidth,
            int? borderRadius
            )   
            :this(imageUrl, thumbnailUrl, caption)
        {
            if (duration.HasValue)
            {
                Duration = duration.Value;
            }
            if (endDelay.HasValue)
            {
                EndDelay = endDelay.Value;
            }
            if (animationBegin.HasValue)
            {
                AnimationBegin = animationBegin.Value;
            }
            if (animationEnd.HasValue)
            {
                AnimationEnd = animationEnd.Value;
            }
            if (centerX.HasValue)
            {
                CenterX = centerX.Value;
            }
            if (centerY.HasValue)
            {
                CenterY = centerY.Value;
            }
            if (scale.HasValue)
            {
                Scale = scale.Value;
            }
            if (rotateAngle.HasValue)
            {
                RotateAngle = rotateAngle.Value;
            }
            if (borderVisible.HasValue)
            {
                BorderVisible = borderVisible.Value;
            }
            if (borderWidth.HasValue)
            {
                BorderWidth = borderWidth.Value;
            }
            if (borderRadius.HasValue)
            {
                BorderRadius = borderRadius.Value;
            }
            BorderColor = borderColor;
        }

        protected virtual void CreateInstance(string imageUrl, string thumbUrl, string caption)
        {
            CreateInstance(new Uri(imageUrl, UriKind.RelativeOrAbsolute), new Uri(thumbUrl, UriKind.RelativeOrAbsolute), caption);
        }

        protected virtual void CreateInstance(Uri imageUrl, Uri thumbUrl, string caption)
        {
            pImageUri = imageUrl;
            pThumbUri = thumbUrl;
            pCaption = caption;
            InitializeHandlers();
        }

        ~Slide()
        {
            FinalizeHandlers();
        }

        protected virtual void InitializeHandlers()
        {
        }

        protected virtual void FinalizeHandlers()
        {
        }

        #endregion

        #region Property accessors

        public Uri ImageUri
        {
            get
            {
                return pImageUri;
            }
            set
            {
                pImageUri = value;
            }
        }

        public Uri ThumbnailUri
        {
            get
            {
                return pThumbUri;
            }
            set
            {
                pThumbUri = value;
            }
        }

        public string Caption
        {
            get
            {
                return pCaption;
            }
            set
            {
                pCaption = value;
            }
        }

        public int? Duration
        {
            get
            {
                return pDuration;
            }
            set
            {
                pDuration = value;
            }
        }

        public int? EndDelay
        {
            get
            {
                return pEndDelay;
            }
            set
            {
                pEndDelay = value;
            }
        }

        public int? AnimationBegin
        {
            get
            {
                return pAnimationBegin;
            }
            set
            {
                pAnimationBegin = value;
            }
        }

        public int? AnimationEnd
        {
            get
            {
                return pAnimationEnd;
            }
            set
            {
                pAnimationEnd = value;
            }
        }

        public double? CenterX
        {
            get
            {
                return pCenterX;
            }
            set
            {
                pCenterX = value;
            }
        }

        public double? CenterY
        {
            get
            {
                return pCenterX;
            }
            set
            {
                pCenterY = value;
            }
        }

        public double? Scale
        {
            get
            {
                return pScale;
            }
            set
            {
                pScale = value;
            }
        }

        public double? RotateAngle
        {
            get
            {
                return pRotateAngle;
            }
            set
            {
                pRotateAngle = value;
            }
        }

        public bool? BorderVisible
        {
            get
            {
                return pBorderVisible;
            }
            set
            {
                pBorderVisible = value;
            }
        }

        public string BorderColor
        {
            get
            {
                return pBorderColor;
            }
            set
            {
                pBorderColor = value;
            }
        }

        public int? BorderWidth
        {
            get
            {
                return pBorderWidth;
            }
            set
            {
                pBorderWidth = value;
            }
        }

        public int? BorderRadius
        {
            get
            {
                return pBorderRadius;
            }
            set
            {
                pBorderRadius = value;
            }
        }

        public int OrderIndex
        {
            get
            {
                return pOrderIndex;
            }
            set
            {
                pOrderIndex = value;
            }
        }

        //public int SlideId
        //{
        //    get
        //    {
        //        return pSlideId;
        //    }
        //    set
        //    {
        //        pSlideId = value;
        //    }
        //}

        //public int GalleryId
        //{
        //    get
        //    {
        //        return pGalleryId;
        //    }
        //    set
        //    {
        //        pGalleryId = value;
        //    }
        //}

        #endregion

        #region Methods

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("<slide");
            if (pImageUri != null)
            {
                sb.AppendFormat(" imageUrl=\"{0}\"", pImageUri);
            }
            if (pThumbUri != null)
            {
                sb.AppendFormat(" thumbnailUrl=\"{0}\"", pThumbUri);
            }
            if (!string.IsNullOrEmpty(pCaption))
            {
                sb.AppendFormat(" caption=\"{0}\"", pCaption);
            }
            if (pDuration.HasValue)
            {
                sb.AppendFormat(" duration=\"{0}\"", pDuration);
            }
            if (pEndDelay.HasValue)
            {
                sb.AppendFormat(" endDelay=\"{0}\"", pEndDelay);
            }
            if (pAnimationBegin.HasValue)
            {
                sb.AppendFormat(" animationBegin=\"{0}\"", pAnimationBegin);
            }
            if (pAnimationEnd.HasValue)
            {
                sb.AppendFormat(" animationEnd=\"{0}\"", pAnimationEnd);
            }
            if (pCenterX.HasValue)
            {
                sb.AppendFormat(" centerX=\"{0}\"", pCenterX.Value.ToString(NumberFormatProvider));
            }
            if (pCenterX.HasValue)
            {
                sb.AppendFormat(" centerY=\"{0}\"", pCenterY.Value.ToString(NumberFormatProvider));
            }
            if (pScale.HasValue)
            {
                sb.AppendFormat(" scale=\"{0}\"", pScale.Value.ToString(NumberFormatProvider));
            }
            if (pRotateAngle.HasValue)
            {
                sb.AppendFormat(" rotateAngle=\"{0}\"", pRotateAngle.Value.ToString(NumberFormatProvider));
            }
            if (pBorderColor != null)
            {
                sb.AppendFormat(" borderColor=\"{0}\"", pBorderColor);
            }
            if (pBorderRadius.HasValue)
            {
                sb.AppendFormat(" borderRadius=\"{0}\"", pBorderRadius);
            }
            if (pBorderVisible.HasValue)
            {
                sb.AppendFormat(" borderVisible=\"{0}\"", pBorderVisible);
            }
            if (pBorderWidth.HasValue)
            {
                sb.AppendFormat(" borderWidth=\"{0}\"", pBorderWidth);
            }
            sb.AppendLine(" />");
            return sb.ToString();
        }

        #region IComparable<PhotoGallerySlideInfo> Members

        public int CompareTo(Slide other)
        {
            return OrderIndex.CompareTo(other.OrderIndex);
        }

        #endregion

        #endregion
        
    }

}